package com.labmi.utility;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class FacesUtils {

	
	public static HttpServletRequest getHttpRequest()
	{
		return (HttpServletRequest) FacesContext.getCurrentInstance().
								getExternalContext().getRequest();
	}
	
	/**
	 * 
	 * @param paramName
	 * @return null if paramName is not a valid long
	 */
	public static Long getParameterAsLong(String paramName)
	{
		String longParam = getHttpRequest().getParameter(paramName);
		Long paramAsLong = null;
		
		try {
			paramAsLong = Long.parseLong(longParam);
		} catch (NumberFormatException e) {
			System.err.println("Url paramter '" + paramName +"' is not a valid Long");
			
		}
		
		return paramAsLong;
	}
	
	public static Integer getParameterAsInt(String paramName)
	{
		String intParam = getHttpRequest().getParameter(paramName);
		Integer paramAsInt = null;
		
		try {
			paramAsInt = Integer.parseInt(intParam);
		} catch (NumberFormatException e) {
			
			System.err.println("Url paramter '" + paramName +"' is not a valid Integer");
			
		}
		
		return paramAsInt;
	}
	
}
